home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 1999 July: Mac OS SDK / Dev.CD Jul 99 SDK1.toast / Development Kits / Mac OS / OpenGL 1.0 SDK / Source / Examples / aux / samples / sphere.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-05-18  |  16.9 KB  |  1,017 lines  |  [TEXT/CWIE]

  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <math.h>
  4. #include <stdlib.h>
  5. #include "tk.h"
  6.  
  7. #ifdef __MWERKS__
  8. #include <console.h>            /* ccommand */
  9. #endif
  10.  
  11. #ifndef FALSE
  12. #define FALSE 0
  13. #define TRUE  1
  14. #endif
  15.  
  16. #define PI    3.14159265358979323846
  17.  
  18.  
  19. GLenum doubleBuffer = 1;
  20. GLenum directRender = 1;
  21. int W = 400, H = 400;
  22.  
  23. char *imageFileName = 0;
  24. TK_RGBImageRec *image;
  25.  
  26. int numComponents;
  27.  
  28. float *minFilter, *magFilter, *sWrapMode, *tWrapMode;
  29. float decal[] = {GL_DECAL};
  30. float modulate[] = {GL_MODULATE};
  31. float replace[] = {GL_REPLACE};
  32. float repeat[] = {GL_REPEAT};
  33. float clamp[] = {GL_CLAMP};
  34. float nearest[] = {GL_NEAREST};
  35. float linear[] = {GL_LINEAR};
  36. float nearest_mipmap_nearest[] = {GL_NEAREST_MIPMAP_NEAREST};
  37. float nearest_mipmap_linear[] = {GL_NEAREST_MIPMAP_LINEAR};
  38. float linear_mipmap_nearest[] = {GL_LINEAR_MIPMAP_NEAREST};
  39. float linear_mipmap_linear[] = {GL_LINEAR_MIPMAP_LINEAR};
  40. GLint sphereMap[] = {GL_SPHERE_MAP};
  41.  
  42. float xRotation = 0.0, yRotation = 0.0;
  43. float zTranslate = -4.0;
  44. GLenum autoRotate = TRUE;
  45. GLenum deepestColor = TK_GREEN;
  46. GLenum isLit = TRUE;
  47. GLenum isFogged = FALSE;
  48. float *textureEnvironment = modulate;
  49.  
  50. struct MipMap {
  51.     int width, height;
  52.     unsigned char *data;
  53. };
  54.  
  55. int cube, cage, cylinder, torus, genericObject;
  56.  
  57. float c[6][4][4][3] = {
  58.     {
  59.     {
  60.         {
  61.         1.0, 1.0, -1.0
  62.         }, 
  63.         {
  64.         0.0, 1.0, -1.0
  65.         },
  66.         {
  67.         0.0, 0.0, -1.0
  68.         },
  69.         {
  70.         1.0, 0.0, -1.0
  71.         },
  72.     },
  73.     {
  74.         {
  75.         0.0, 1.0, -1.0
  76.         },
  77.         {
  78.         -1.0, 1.0, -1.0
  79.         }, 
  80.         {
  81.         -1.0, 0.0, -1.0
  82.         }, 
  83.         {
  84.         0.0, 0.0, -1.0
  85.         },
  86.     },
  87.     {
  88.         {
  89.         0.0,  0.0, -1.0
  90.         },
  91.         {
  92.         -1.0, 0.0, -1.0
  93.         },
  94.         {
  95.         -1.0, -1.0, -1.0
  96.         },
  97.         {
  98.         0.0, -1.0, -1.0
  99.         },
  100.     },
  101.     {
  102.         {
  103.         1.0, 0.0, -1.0
  104.         },
  105.         {
  106.         0.0, 0.0, -1.0
  107.         },
  108.         {
  109.         0.0, -1.0, -1.0
  110.         },
  111.         {
  112.         1.0, -1.0, -1.0
  113.         },
  114.     },
  115.     },
  116.     {
  117.     {
  118.         {
  119.         1.0, 1.0, 1.0
  120.         },
  121.         {
  122.         1.0, 1.0, 0.0
  123.         },
  124.         {
  125.         1.0, 0.0, 0.0
  126.         },
  127.         {
  128.         1.0, 0.0, 1.0
  129.         },
  130.     },
  131.     {
  132.         {
  133.         1.0, 1.0, 0.0
  134.         },
  135.         {
  136.         1.0, 1.0, -1.0
  137.         },
  138.         {
  139.         1.0, 0.0, -1.0
  140.         },
  141.         {
  142.         1.0, 0.0, 0.0
  143.         },
  144.     },
  145.     {
  146.         {
  147.         1.0, 0.0, -1.0
  148.         },
  149.         {
  150.         1.0, -1.0, -1.0
  151.         },
  152.         {
  153.         1.0, -1.0, 0.0
  154.         },
  155.         {
  156.         1.0, 0.0, 0.0
  157.         },
  158.     },
  159.     {
  160.         {
  161.         1.0, 0.0, 0.0
  162.         },
  163.         {
  164.         1.0, -1.0, 0.0
  165.         },
  166.         {
  167.         1.0, -1.0, 1.0
  168.         },
  169.         {
  170.         1.0, 0.0, 1.0
  171.         },
  172.     },
  173.     },
  174.     {
  175.     {
  176.         {
  177.         -1.0, 1.0, 1.0
  178.         },
  179.         {
  180.         0.0, 1.0, 1.0
  181.         },
  182.         {
  183.         0.0, 0.0, 1.0
  184.         },
  185.         {
  186.         -1.0, 0.0, 1.0
  187.         },
  188.     },
  189.     {
  190.         {
  191.         0.0, 1.0, 1.0
  192.         },
  193.         {
  194.         1.0, 1.0, 1.0
  195.         },
  196.         {
  197.         1.0, 0.0, 1.0
  198.         },
  199.         {
  200.         0.0, 0.0, 1.0
  201.         },
  202.     },
  203.     {
  204.         {
  205.         1.0, 0.0, 1.0
  206.         },
  207.         {
  208.         1.0, -1.0, 1.0
  209.         },
  210.         {
  211.         0.0, -1.0, 1.0
  212.         },
  213.         {
  214.         0.0, 0.0, 1.0
  215.         },
  216.     },
  217.     {
  218.         {
  219.         0.0, -1.0, 1.0
  220.         },
  221.         {
  222.         -1.0, -1.0, 1.0
  223.         },
  224.         {
  225.         -1.0, 0.0, 1.0
  226.         },
  227.         {
  228.         0.0, 0.0, 1.0
  229.         },
  230.     },
  231.     },
  232.     {
  233.     {
  234.         {
  235.         -1.0, 1.0, -1.0
  236.         },
  237.         {
  238.         -1.0, 1.0, 0.0
  239.         },
  240.         {
  241.         -1.0, 0.0, 0.0
  242.         },
  243.         {
  244.         -1.0, 0.0, -1.0
  245.         },
  246.     }, 
  247.     {
  248.         {
  249.         -1.0, 1.0, 0.0
  250.         },
  251.         {
  252.         -1.0, 1.0, 1.0
  253.         },
  254.         {
  255.         -1.0, 0.0, 1.0
  256.         },
  257.         {
  258.         -1.0, 0.0, 0.0
  259.         },
  260.     }, 
  261.     {
  262.         {
  263.         -1.0, 0.0, 1.0
  264.         },
  265.         {
  266.         -1.0, -1.0, 1.0
  267.         },
  268.         {
  269.         -1.0, -1.0, 0.0
  270.         },
  271.         {
  272.         -1.0, 0.0, 0.0
  273.         },
  274.     }, 
  275.     {
  276.         {
  277.         -1.0, -1.0, 0.0
  278.         },
  279.         {
  280.         -1.0, -1.0, -1.0
  281.         },
  282.         {
  283.         -1.0, 0.0, -1.0
  284.         },
  285.         {
  286.         -1.0, 0.0, 0.0
  287.         },
  288.     }, 
  289.     },
  290.     {
  291.     {
  292.         {
  293.         -1.0, 1.0, 1.0
  294.         },
  295.         {
  296.         -1.0, 1.0, 0.0
  297.         },
  298.         {
  299.         0.0, 1.0, 0.0
  300.         },
  301.         {
  302.         0.0, 1.0, 1.0
  303.         },
  304.     },
  305.     {
  306.         {
  307.         -1.0, 1.0, 0.0
  308.         },
  309.         {
  310.         -1.0, 1.0, -1.0
  311.         },
  312.         {
  313.         0.0, 1.0, -1.0
  314.         },
  315.         {
  316.         0.0, 1.0, 0.0
  317.         },
  318.     },
  319.     {
  320.         {
  321.         0.0, 1.0, -1.0
  322.         },
  323.         {
  324.         1.0, 1.0, -1.0
  325.         },
  326.         {
  327.         1.0, 1.0, 0.0
  328.         },
  329.         {
  330.         0.0, 1.0, 0.0
  331.         },
  332.     },
  333.     {
  334.         {
  335.         1.0, 1.0, 0.0
  336.         },
  337.         {
  338.         1.0, 1.0, 1.0
  339.         },
  340.         {
  341.         0.0, 1.0, 1.0
  342.         },
  343.         {
  344.         0.0, 1.0, 0.0
  345.         },
  346.     },
  347.     },
  348.     {
  349.     {
  350.         {
  351.         -1.0, -1.0, -1.0
  352.         },
  353.         {
  354.         -1.0, -1.0, 0.0
  355.         },
  356.         {
  357.         0.0, -1.0, 0.0
  358.         },
  359.         {
  360.         0.0, -1.0, -1.0
  361.         },
  362.     },
  363.     {
  364.         {
  365.         -1.0, -1.0, 0.0
  366.         },
  367.         {
  368.         -1.0, -1.0, 1.0
  369.         },
  370.         {
  371.         0.0, -1.0, 1.0
  372.         },
  373.         {
  374.         0.0, -1.0, 0.0
  375.         },
  376.     },
  377.     {
  378.         {
  379.         0.0, -1.0, 1.0
  380.         },
  381.         {
  382.         1.0, -1.0, 1.0
  383.         },
  384.         {
  385.         1.0, -1.0, 0.0
  386.         },
  387.         {
  388.         0.0, -1.0, 0.0
  389.         },
  390.     },
  391.     {
  392.         {
  393.         1.0, -1.0, 0.0
  394.         },
  395.         {
  396.         1.0, -1.0, -1.0
  397.         },
  398.         {
  399.         0.0, -1.0, -1.0
  400.         },
  401.         {
  402.         0.0, -1.0, 0.0
  403.         },
  404.     },
  405.     }
  406. };
  407.  
  408. float n[6][3] = {
  409.     {
  410.     0.0, 0.0, -1.0
  411.     },
  412.     {
  413.     1.0, 0.0, 0.0
  414.     },
  415.     {
  416.     0.0, 0.0, 1.0
  417.     },
  418.     {
  419.     -1.0, 0.0, 0.0
  420.     },
  421.     {
  422.     0.0, 1.0, 0.0
  423.     },
  424.     {
  425.     0.0, -1.0, 0.0
  426.     }
  427. };
  428.  
  429. GLfloat identity[16] = {
  430.     1, 0, 0, 0,
  431.     0, 1, 0, 0,
  432.     0, 0, 1, 0,
  433.     0, 0, 0, 1,
  434. };
  435.  
  436.  
  437. void BuildCylinder(int numEdges)
  438. {
  439.     int i, top = 1.0, bottom = -1.0;
  440.     float x[100], y[100], angle; 
  441.     
  442.     for (i = 0; i <= numEdges; i++) {
  443.     angle = i * 2.0 * PI / numEdges;
  444.     x[i] = cosf(angle);
  445.     y[i] = sinf(angle);
  446.     }
  447.  
  448.     glNewList(cylinder, GL_COMPILE);
  449.     glBegin(GL_TRIANGLE_STRIP);
  450.     for (i = 0; i <= numEdges; i++) {
  451.         glNormal3f(x[i], y[i], 0.0);
  452.         glVertex3f(x[i], y[i], bottom);
  453.         glVertex3f(x[i], y[i], top);
  454.     }
  455.     glEnd();
  456.     glBegin(GL_TRIANGLE_FAN);
  457.     glNormal3f(0.0, 0.0, 1.0);
  458.     glVertex3f(0.0, 0.0, top);
  459.     for (i = 0; i <= numEdges; i++) {
  460.         glVertex3f(x[i], -y[i], top);
  461.     }
  462.     glEnd();
  463.     glBegin(GL_TRIANGLE_FAN);
  464.     glNormal3f(0.0, 0.0, -1.0);
  465.     glVertex3f(0.0, 0.0, bottom);
  466.     for (i = 0; i <= numEdges; i++) {
  467.         glVertex3f(x[i], y[i], bottom);
  468.     }
  469.     glEnd();
  470.     glEndList();
  471. }
  472.  
  473. void BuildTorus(float rc, int numc, float rt, int numt)
  474. {
  475.     int i, j, k;
  476.     double s, t;
  477.     double x, y, z;
  478.     double pi, twopi;
  479.  
  480.     pi = 3.14159265358979323846;
  481.     twopi = 2.0 * pi;
  482.  
  483.     glNewList(torus, GL_COMPILE);
  484.     for (i = 0; i < numc; i++) {
  485.     glBegin(GL_QUAD_STRIP);
  486.         for (j = 0; j <= numt; j++) {
  487.         for (k = 0; k <= 1; k++) {
  488.         s = (i + k) % numc + 0.5;
  489.         t = j % numt;
  490.  
  491.         x = cos(t*twopi/numt) * cos(s*twopi/numc);
  492.         y = sin(t*twopi/numt) * cos(s*twopi/numc);
  493.         z = sin(s*twopi/numc);
  494.         glNormal3f(x, y, z);
  495.  
  496.         x = (rt + rc * cos(s*twopi/numc)) * cos(t*twopi/numt);
  497.         y = (rt + rc * cos(s*twopi/numc)) * sin(t*twopi/numt);
  498.         z = rc * sin(s*twopi/numc);
  499.         glVertex3f(x, y, z);
  500.         }
  501.         }
  502.     glEnd();
  503.     }
  504.     glEndList();
  505. }
  506.  
  507. void BuildCage(void)
  508. {
  509.     int i, j;
  510.     float inc;
  511.     float right, left, top, bottom, front, back;
  512.  
  513.     front  = 0.0;
  514.     back   = -8.0;
  515.  
  516.     left   = -4.0;
  517.     bottom = -4.0;
  518.     right  = 4.0;
  519.     top    = 4.0; 
  520.  
  521.     inc = 2.0 * 4.0 * 0.1;
  522.  
  523.     glNewList(cage, GL_COMPILE);
  524.     for (i = 0; i < 10; i++) {
  525.  
  526.     /*
  527.     ** Back
  528.     */
  529.     glBegin(GL_LINES);
  530.         glVertex3f(left+i*inc, top,    back);
  531.         glVertex3f(left+i*inc, bottom, back);
  532.     glEnd();
  533.     glBegin(GL_LINES);
  534.         glVertex3f(right, bottom+i*inc, back);
  535.         glVertex3f(left,  bottom+i*inc, back);
  536.     glEnd();
  537.  
  538.     /*
  539.     ** Front
  540.     */
  541.     glBegin(GL_LINES);
  542.         glVertex3f(left+i*inc, top,    front);
  543.         glVertex3f(left+i*inc, bottom, front);
  544.     glEnd();
  545.     glBegin(GL_LINES);
  546.         glVertex3f(right, bottom+i*inc, front);
  547.         glVertex3f(left,  bottom+i*inc, front);
  548.     glEnd();
  549.  
  550.     /*
  551.     ** Left
  552.     */
  553.     glBegin(GL_LINES);
  554.         glVertex3f(left, bottom+i*inc, front);
  555.         glVertex3f(left, bottom+i*inc, back);
  556.     glEnd();
  557.     glBegin(GL_LINES);
  558.         glVertex3f(left, top,    back+i*inc);
  559.         glVertex3f(left, bottom, back+i*inc);
  560.     glEnd();
  561.  
  562.     /*
  563.     ** Right
  564.     */
  565.     glBegin(GL_LINES);
  566.         glVertex3f(right, top-i*inc, front);
  567.         glVertex3f(right, top-i*inc, back);
  568.     glEnd();
  569.     glBegin(GL_LINES);
  570.         glVertex3f(right, top,    back+i*inc);
  571.         glVertex3f(right, bottom, back+i*inc);
  572.     glEnd();
  573.  
  574.     /*
  575.     ** Top
  576.     */
  577.     glBegin(GL_LINES);
  578.         glVertex3f(left+i*inc, top, front);
  579.         glVertex3f(left+i*inc, top, back);
  580.     glEnd();
  581.     glBegin(GL_LINES);
  582.         glVertex3f(right, top, back+i*inc);
  583.         glVertex3f(left,  top, back+i*inc);
  584.     glEnd();
  585.  
  586.     /*
  587.     ** Bottom
  588.     */
  589.     glBegin(GL_LINES);
  590.         glVertex3f(right-i*inc, bottom, front);
  591.         glVertex3f(right-i*inc, bottom, back);
  592.     glEnd();
  593.     glBegin(GL_LINES);
  594.         glVertex3f(right, bottom, back+i*inc);
  595.         glVertex3f(left,  bottom, back+i*inc);
  596.     glEnd();
  597.     }
  598.     glEndList();
  599. }
  600.  
  601. void BuildCube(void)
  602. {
  603.     int i, j;
  604.  
  605.     glNewList(cube, GL_COMPILE);
  606.     for (i = 0; i < 6; i++) {
  607.     for (j = 0; j < 4; j++) {
  608.         glNormal3fv(n[i]); 
  609.         glBegin(GL_POLYGON);
  610.         glVertex3fv(c[i][j][0]);
  611.         glVertex3fv(c[i][j][1]);
  612.         glVertex3fv(c[i][j][2]);
  613.         glVertex3fv(c[i][j][3]);
  614.         glEnd();
  615.     }
  616.     }
  617.     glEndList();
  618. }
  619.  
  620. void BuildLists(void)
  621. {
  622.  
  623.     cube = glGenLists(1);
  624.     BuildCube();
  625.  
  626.     cage = glGenLists(2);
  627.     BuildCage();
  628.  
  629.     cylinder = glGenLists(3);
  630.     BuildCylinder(60);
  631.  
  632.     torus = glGenLists(4);
  633.     BuildTorus(0.65, 20, .85, 65);
  634.  
  635.     genericObject = torus;
  636. }
  637.  
  638. void SetDeepestColor(void)
  639. {
  640.     GLint redBits, greenBits, blueBits;
  641.  
  642.     glGetIntegerv(GL_RED_BITS, &redBits);
  643.     glGetIntegerv(GL_GREEN_BITS, &greenBits);
  644.     glGetIntegerv(GL_BLUE_BITS, &blueBits);
  645.  
  646.     deepestColor = (redBits >= greenBits) ? TK_RED : TK_GREEN;
  647.     deepestColor = (deepestColor >= blueBits) ? deepestColor : TK_BLUE; 
  648. }
  649.  
  650. void SetDefaultSettings(void)
  651. {
  652.  
  653.     magFilter = nearest;
  654.     minFilter = nearest;
  655.     sWrapMode = repeat;
  656.     tWrapMode = repeat;
  657.     textureEnvironment = modulate;
  658.     autoRotate = TRUE;
  659. }
  660.  
  661. unsigned char *AlphaPadImage(int bufSize, unsigned char *inData, int alpha)
  662. {
  663.     unsigned char *outData, *out_ptr, *in_ptr;
  664.     int i;
  665.  
  666.     outData = (unsigned char *) malloc(bufSize * 4);
  667.     out_ptr = outData;
  668.     in_ptr = inData;
  669.  
  670.     for (i = 0; i < bufSize; i++) {
  671.     *out_ptr++ = *in_ptr++;
  672.     *out_ptr++ = *in_ptr++;
  673.     *out_ptr++ = *in_ptr++;
  674.     *out_ptr++ = alpha;
  675.     }
  676.  
  677.     free (inData);
  678.     return outData;
  679. }
  680.  
  681. void Init(void)
  682. {
  683.     float ambient[] = {0.0, 0.0, 0.0, 1.0};
  684.     float diffuse[] = {0.0, 1.0, 0.0, 1.0};
  685.     float specular[] = {1.0, 1.0, 1.0, 1.0};
  686.     float position[] = {2.0, 2.0,  0.0, 1.0};
  687.     float fog_color[] = {0.0, 0.0, 0.0, 1.0};
  688.     float mat_ambient[] = {0.0, 0.0, 0.0, 1.0};
  689.     float mat_shininess[] = {90.0};
  690.     float mat_specular[] = {1.0, 1.0, 1.0, 1.0};
  691.     float mat_diffuse[] = {1.0, 1.0, 1.0, 1.0};
  692.     float lmodel_ambient[] = {0.0, 0.0, 0.0, 1.0};
  693.     float lmodel_twoside[] = {GL_TRUE};
  694.     float lmodel_local_viewer[] = {GL_FALSE};
  695.  
  696.     SetDeepestColor();
  697.     SetDefaultSettings();
  698.  
  699.     if(imageFileName)
  700.     {
  701.         if (numComponents == 4) {
  702.         image = tkRGBImageLoad(imageFileName);
  703.         image->data = AlphaPadImage(image->sizeX*image->sizeY,
  704.                                         image->data, 128);
  705.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  706.         gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  707.                   image->sizeX, image->sizeY, 
  708.                   GL_RGBA, GL_UNSIGNED_BYTE, image->data);
  709.         } else {
  710.         image = tkRGBImageLoad(imageFileName);
  711.         glPixelStorei(GL_UNPACK_ALIGNMENT, 1);
  712.         gluBuild2DMipmaps(GL_TEXTURE_2D, numComponents, 
  713.                   image->sizeX, image->sizeY, 
  714.                   GL_RGB, GL_UNSIGNED_BYTE, image->data);
  715.         }
  716.     }
  717.  
  718.     glFogf(GL_FOG_DENSITY, 0.125);
  719.     glFogi(GL_FOG_MODE, GL_LINEAR);
  720.     glFogf(GL_FOG_START, 4.0);
  721.     glFogf(GL_FOG_END, 9.0);
  722.     glFogfv(GL_FOG_COLOR, fog_color);
  723.  
  724.     glLightfv(GL_LIGHT0, GL_AMBIENT, ambient);
  725.     glLightfv(GL_LIGHT0, GL_DIFFUSE, diffuse);
  726.     glLightfv(GL_LIGHT0, GL_SPECULAR, specular);
  727.     glLightfv(GL_LIGHT0, GL_POSITION, position);
  728.     
  729.     glMaterialfv(GL_FRONT_AND_BACK, GL_SHININESS, mat_shininess);
  730.     glMaterialfv(GL_FRONT_AND_BACK, GL_SPECULAR, mat_specular);
  731.     glMaterialfv(GL_FRONT_AND_BACK, GL_DIFFUSE, mat_diffuse);
  732.     glMaterialfv(GL_FRONT_AND_BACK, GL_AMBIENT, mat_ambient);
  733.  
  734.     glLightModelfv(GL_LIGHT_MODEL_AMBIENT, lmodel_ambient);
  735.     glLightModelfv(GL_LIGHT_MODEL_TWO_SIDE, lmodel_twoside);
  736.     glShadeModel(GL_SMOOTH);
  737.  
  738.     glEnable(GL_LIGHTING);
  739.     glEnable(GL_LIGHT0);
  740.  
  741.     glClearColor(0.0, 0.0, 0.0, 0.0);
  742.     glViewport(0, 0, W, H);
  743.     glEnable(GL_DEPTH_TEST);
  744.  
  745.     glFrontFace(GL_CW);
  746.     glEnable(GL_CULL_FACE);
  747.     glCullFace(GL_BACK);
  748.  
  749.     if(imageFileName)
  750.     {
  751.         glEnable(GL_TEXTURE_2D);
  752.         glTexGeniv(GL_S, GL_TEXTURE_GEN_MODE, sphereMap);
  753.         glTexGeniv(GL_T, GL_TEXTURE_GEN_MODE, sphereMap);
  754.         glEnable(GL_TEXTURE_GEN_S);
  755.         glEnable(GL_TEXTURE_GEN_T);
  756.  
  757.         glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  758.         glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  759.         glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, sWrapMode);
  760.         glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, tWrapMode);
  761.  
  762.         glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  763.     }
  764.  
  765.     BuildLists();
  766. }
  767.  
  768. void ReInit(void)
  769. {
  770.  
  771.     if (genericObject == torus) {
  772.     glEnable(GL_DEPTH_TEST);
  773.     } else  {
  774.     glDisable(GL_DEPTH_TEST);
  775.     }
  776.     if (isFogged) {
  777.     textureEnvironment = modulate;
  778.     }
  779.  
  780.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, magFilter);
  781.     glTexParameterfv(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, minFilter);
  782.     glTexEnvfv(GL_TEXTURE_ENV, GL_TEXTURE_ENV_MODE, textureEnvironment);
  783. }
  784.  
  785. void Draw(void)
  786. {
  787.  
  788.     glMatrixMode(GL_PROJECTION);
  789.     glLoadIdentity();
  790.     glFrustum(-0.2, 0.2, -0.2, 0.2, 0.15, 9.0);
  791.     glMatrixMode(GL_MODELVIEW);
  792.  
  793.     glClear(GL_COLOR_BUFFER_BIT|GL_DEPTH_BUFFER_BIT);
  794.     if (isFogged) {
  795.     glEnable(GL_FOG);
  796.     glColor3fv(tkRGBMap[deepestColor]);
  797.     } else {
  798.     glColor3fv(tkRGBMap[TK_WHITE]);
  799.     }
  800.     glDisable(GL_LIGHTING);
  801.     glDisable(GL_LIGHT0);
  802.     glDisable(GL_TEXTURE_2D);
  803.     glCallList(cage);
  804.  
  805.     glPushMatrix();
  806.     glTranslatef(0.0, 0.0, zTranslate);
  807.     glRotatef(xRotation, 1, 0, 0);
  808.     glRotatef(yRotation, 0, 1, 0);
  809.  
  810.     if (isLit == TRUE) {
  811.     glEnable(GL_LIGHTING);
  812.     glEnable(GL_LIGHT0);
  813.     }
  814.  
  815.     glEnable(GL_TEXTURE_2D);
  816.     if (isFogged) {
  817.     glDisable(GL_FOG);
  818.     }
  819.     glPolygonMode(GL_FRONT, GL_FILL);
  820.     glColor3fv(tkRGBMap[deepestColor]);
  821.     glCallList(genericObject);
  822.  
  823.     glPopMatrix();
  824.     glFlush();
  825.  
  826.     if (autoRotate) {
  827.     xRotation += 1.0;
  828.     yRotation += .75;
  829.     }
  830.     
  831.     tkSwapBuffers();
  832. }
  833.  
  834. void Reshape(int width, int height)
  835. {
  836.  
  837.     W = width;
  838.     H = height;
  839.     ReInit();
  840. }
  841.  
  842. GLenum Key(int key, GLenum mask)
  843. {
  844.  
  845.     switch (key) {
  846.       case TK_ESCAPE:
  847.     free(image->data);
  848.     tkQuit();
  849.  
  850.       case TK_LEFT:
  851.     yRotation -= 0.5;
  852.     autoRotate = FALSE;
  853.     ReInit();
  854.     break;
  855.       case TK_RIGHT:
  856.     yRotation += 0.5;
  857.     autoRotate = FALSE;
  858.     ReInit();
  859.     break;
  860.       case TK_UP:
  861.     xRotation -= 0.5;
  862.     autoRotate = FALSE;
  863.     ReInit();
  864.     break;
  865.       case TK_DOWN:
  866.     xRotation += 0.5;
  867.     autoRotate = FALSE;
  868.     ReInit();
  869.     break;
  870.       case TK_a:
  871.     autoRotate = !autoRotate;
  872.     ReInit();
  873.     break;
  874.       case TK_c:
  875.     genericObject = (genericObject == cube) ? cylinder : cube;
  876.     ReInit();
  877.     break;
  878.       case TK_d:
  879.     textureEnvironment = decal;
  880.     ReInit();
  881.     break;
  882.       case TK_m:
  883.     textureEnvironment = modulate;
  884.     ReInit();
  885.     break;
  886.     case TK_r:
  887.     textureEnvironment = replace;
  888.     ReInit();
  889.     break;
  890.       case TK_l:
  891.     isLit = !isLit;
  892.     ReInit();
  893.     break;
  894.       case TK_f:
  895.     isFogged = !isFogged;
  896.     ReInit();
  897.     break;
  898.       case TK_t:
  899.     genericObject = torus;
  900.     ReInit();
  901.     break;
  902.       case TK_0:
  903.     magFilter = nearest;
  904.     ReInit();
  905.     break;
  906.       case TK_1:
  907.     magFilter = linear;
  908.     ReInit();
  909.     break;
  910.       case TK_2:
  911.     minFilter = nearest;
  912.     ReInit();
  913.     break;
  914.       case TK_3:
  915.     minFilter = linear;
  916.     ReInit();
  917.     break;
  918.       case TK_4:
  919.     minFilter = nearest_mipmap_nearest;
  920.     ReInit();
  921.     break;
  922.       case TK_5:
  923.     minFilter = nearest_mipmap_linear;
  924.     ReInit();
  925.     break;
  926.       case TK_6:
  927.     minFilter = linear_mipmap_nearest;
  928.     ReInit();
  929.     break;
  930.       case TK_7:
  931.     minFilter = linear_mipmap_linear;
  932.     ReInit();
  933.     break;
  934.  
  935.       default:
  936.     return GL_FALSE;
  937.     }
  938.     return GL_TRUE;
  939. }
  940.  
  941. GLenum Args(int argc, char **argv)
  942. {
  943.     GLint i;
  944.  
  945.     doubleBuffer = GL_TRUE;
  946.     directRender = GL_TRUE;
  947.     numComponents = 4;
  948.  
  949.     for (i = 1; i < argc; i++) {
  950.     if (strcmp(argv[i], "-sb") == 0) {
  951.         doubleBuffer = GL_FALSE;
  952.     } else if (strcmp(argv[i], "-db") == 0) {
  953.         doubleBuffer = GL_TRUE;
  954.     } else if (strcmp(argv[i], "-dr") == 0) {
  955.         directRender = GL_TRUE;
  956.     } else if (strcmp(argv[i], "-ir") == 0) {
  957.         directRender = GL_FALSE;
  958.     } else if (strcmp(argv[i], "-f") == 0) {
  959.         if (i+1 >= argc || argv[i+1][0] == '-') {
  960.         printf("-f (No file name).\n");
  961.         return GL_FALSE;
  962.         } else {
  963.         imageFileName = argv[++i];
  964.         }
  965.     } else if (strcmp(argv[i], "-4") == 0) {
  966.         numComponents = 4;
  967.     } else if (strcmp(argv[i], "-3") == 0) {
  968.         numComponents = 3;
  969.     } else {
  970.         printf("%s (Bad option).\n", argv[i]);
  971.         return GL_FALSE;
  972.     }
  973.     }
  974.     return GL_TRUE;
  975. }
  976.  
  977. void main(int argc, char **argv)
  978. {
  979.     GLenum type;
  980.     
  981.     #ifdef __MWERKS__
  982.         argc = ccommand(&argv);
  983.     #endif
  984.  
  985.     if(argc > 1)
  986.     {
  987.         if (Args(argc, argv) == GL_FALSE) {
  988.             tkQuit();
  989.         }
  990.  
  991.         if (imageFileName == 0) {
  992.             printf("No image file.\n");
  993.             tkQuit();
  994.         }
  995.     }
  996.  
  997.     tkInitPosition(30, 60, W, H);
  998.  
  999.     type = TK_RGB | TK_DEPTH;
  1000.     type |= (doubleBuffer) ? TK_DOUBLE : TK_SINGLE;
  1001.     type |= (directRender) ? TK_DIRECT : TK_INDIRECT;
  1002.     tkInitDisplayMode(type);
  1003.  
  1004.     if (tkInitWindow("Texture Test") == GL_FALSE) {
  1005.         tkQuit();
  1006.     }
  1007.  
  1008.     Init();
  1009.  
  1010.     tkExposeFunc(Reshape);
  1011.     tkReshapeFunc(Reshape);
  1012.     tkKeyDownFunc(Key);
  1013.     tkIdleFunc(Draw);
  1014.  
  1015.     tkExec();
  1016. }
  1017.